package Offer_35;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/3/30 10:45
 * 复杂链表复制
 * 需要用到map映射
 */
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

class Solution {
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        Node newNodeList = new Node(-1);
        Node last = newNodeList;
        Node cur = head;
        while (cur!=null){

            Node node = new Node(cur.val);
            node.random = cur.random;
            map.put(cur,node);

            last.next = node;
            last = node;

            cur = cur.next;
        }
        Node tem = newNodeList.next;
        while (tem != null){
            if (tem.random != null){
                Node ranNode = map.get(tem.random);
                tem.random = ranNode;
            }
            tem =tem.next;
        }
        return newNodeList.next;
    }
}